'''
Created on 19.11.2009

@author: guest
'''
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from ru.simpleprojecttool.db.model import Participant
from ru.simpleprojecttool.db.model import ParticipantRole
from ru.simpleprojecttool.db.model import Role
from ru.simpleprojecttool.utils import utils

class CheckedRole:
    role = Role
    checked = bool

class ListParticipants(webapp.RequestHandler):
    def get(self):
        project = db.get(db.Key(self.request.get('projectId')))
        if utils.canEditProject(project) == False:
            utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
            return
        participants = Participant.all().filter('project = ', project)
        template_values = {
                           'projectId': self.request.get('projectId'), 
                           'participants': participants
                           }
        utils.showTemplate(self, '../../../templates/user/participants.html', [], template_values)

class CreateParticipant(webapp.RequestHandler):
    def get(self):
        project = db.get(db.Key(self.request.get('projectId')))
        if utils.canEditProject(project) == False:
            utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
            return
        roles = Role.all().filter("project = ", project)
        template_values = {
                           'projectId': self.request.get('projectId'), 
                           'roles': roles
                           }
#        path = os.path.join(os.path.dirname(__file__), '../../../templates/user/createParticipant.html')
#        self.response.out.write(template.render(path, template_values))
        utils.showTemplate(self, '../../../templates/user/createParticipant.html', [], template_values)
       
class CreateParticipantAction(webapp.RequestHandler):
    def post(self):
        project = db.get(db.Key(self.request.get('projectId')))
        if utils.canEditProject(project) == False:
            utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
            return
        roles = Role.all().filter('project = ', project)
        user = users.User(self.request.get('email'))
        participant = Participant(user = user, project = project)
        participant.put()

        for role in roles:
            if self.request.get(str(role.key())) == 'on':
                participantRole = ParticipantRole(role = role, participant = participant)
                participantRole.put()
                
                
        self.redirect('/participants?projectId=%s' % str(project.key()))
        
class EditParticipant(webapp.RequestHandler): 
    def get(self):
        participant = db.get(db.Key(self.request.get('participantId')))
        if utils.canEditProject(participant.project) == False:
            utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
            return
        roles = Role.all().filter('project = ', participant.project)
        participantRoles = ParticipantRole.all().filter('participant = ', participant)
        
        checkedRoles = []
        
        for role in roles:
            checkedRole = CheckedRole()
            checkedRole.checked = False
            checkedRole.role = role
            for pRole in participantRoles:
                if pRole.role.key() == role.key():
                    checkedRole.checked = True
                    break
            checkedRoles.append(checkedRole)
        
        template_values = {
                           'participant': participant, 
                           'roles': checkedRoles
                           } 
        utils.showTemplate(self, '../../../templates/user/editParticipant.html', [], template_values)

class SaveParticipant(webapp.RequestHandler):
    def post(self):
        participant = db.get(db.Key(self.request.get('participantId')))
        if utils.canEditProject(participant.project) == False:
            utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
            return
        participant.user = users.User(self.request.get('email'))
        participant.put()
        
        pRoles = ParticipantRole.all().filter('participant = ', participant)
        roles = Role.all().filter('project = ', participant.project)
        
        
        
        #remove old roles
        for pRole in pRoles:
            if self.request.get(str(pRole.role.key())) != 'on':
                db.delete(pRole)

        #create new roles
        for role in roles:
            if self.request.get(str(role.key())) == 'on' :
                found = False ;
                for pRole in pRoles:
                    if pRole.role == role:
                        found = True
                if found == False:
                    participantRole = ParticipantRole(role = role, participant = participant)
                    participantRole.put()

        self.redirect('/participants?projectId=%s' % str(participant.project.key()))
    
class DeleteParticipant(webapp.RequestHandler):
    def post(self):
        participant = db.get(db.Key(self.request.get('participantId')))
        if utils.canEditProject(participant.project) == False:
            utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
            return
        projectId = participant.project.key()
        db.delete(participant)
        self.redirect('/participants?projectId=%s' % str(projectId))